<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./node_modules/axios/dist/axios.min.js"></script>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            text-align: left;
            padding: 8px;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        a {
            color: blue;
            text-decoration: none;
        }
        a:hover {
            color: red;
        }
        h1{
            text-align: center;
        }
        #addUser{
            display: block;
            text-align: end;
            height: 30px;
            line-height: 30px;
        }
    </style>
</head>
<body>
    <h1>用户列表</h1>
     <a href="./add.html" id="addUser"> 添加用户</a>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>密码</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- <tr>
                <td>1</td>
                <td>admin</td>
                <td>123456</td>
                <td>
                    <a href="./edit.html">编辑</a>
                    <a href="">删除</a>
                   
                </td>
            </tr> -->
        </tbody>
    </table>
    <script>
        const tbody=document.querySelector('tbody')
        axios.get('http://localhost:3000/users').then(res=>{
            const arr=res.data
            tbody.innerHTML=arr.map(item=>{
                return `
                <tr>
                    <td>${item.id}</td>
                    <td>${item.username}</td>
                    <td>${item.password}</td>
                    <td>
                        <a href="./edit.html?id=${item.id}">编辑</a>
                        <a href="" onclick="deleteUser(${item.id})">删除</a>
                    </td>
                </tr>
                `
            }).join('')
        })
        function deleteUser(id){
            const flag=confirm('确认删除吗？')
            if(flag){
                axios.delete(`http://localhost:3000/users/${id}`).then(res=>{
                    alert('删除成功')
                    location.href='./index.html'
                })
            }
        }
    </script>
</body>
</html>